package dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.List;

import base.BaseDialog;
import greendao.entity.User;
import views.ZdfzrView;

/**
 * Created by Ren on 2017/8/25.
 * 指定监护人Dialog
 */

public class ZdfzrDialog extends BaseDialog implements View.OnClickListener {

    LinearLayout zdfzr_layout;
    TextView tv_confirm, tv_cancel, msg;

    onClickListen monClickListen;
    //所有填写的负责人信息
    ArrayList<User> mSelectUserList = new ArrayList<>();

    public ZdfzrDialog(Context context, onClickListen monClickListen) {
        super(context, R.style.Theme_dialog);
        this.monClickListen = monClickListen;
    }

    public ZdfzrDialog(Context context, int themeResId) {
        super(context, themeResId);

    }

    protected ZdfzrDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_zdfzr);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {

        setCanceledOnTouchOutside(false);

        zdfzr_layout = (LinearLayout) findViewById(R.id.zdfzr_layout);
        tv_confirm = (TextView) findViewById(R.id.confirm);
        tv_cancel = (TextView) findViewById(R.id.cancel);
        msg = (TextView) findViewById(R.id.msg);

        if(mSelectUserList.size()==0){
            //默认增加一个指定负责人V
            zdfzr_layout.addView(new ZdfzrView(getContext(), true));
        }
        else {
            for(int i=0;i<mSelectUserList.size();i++){
                if(i==0){
                    ZdfzrView view=new ZdfzrView(getContext(), true);
                    view.setDate(mSelectUserList.get(i));
                    zdfzr_layout.addView(view);
                } else {
                    ZdfzrView view=new ZdfzrView(getContext(), false);
                    view.setDate(mSelectUserList.get(i));
                    zdfzr_layout.addView(view);
                }
            }


        }

    }

    @Override
    protected void InitData() {

    }

    @Override
    protected void InitListen() {
        tv_confirm.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                getSelectUserList();
                monClickListen.onConfirmListen(mSelectUserList);
                break;
            case R.id.cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    public void setMsg(String s) {
        msg.setText(s);
    }

    //获取所有指定负责人view中的负责人数据
    private void getSelectUserList() {
        mSelectUserList.clear();
        for (int i = 0; i < zdfzr_layout.getChildCount(); i++) {
            ZdfzrView mZdfzrView = (ZdfzrView) zdfzr_layout.getChildAt(i);
            if (mZdfzrView.getSelectUser() != null) {
                mSelectUserList.add(mZdfzrView.getSelectUser());
            }
        }

    }

    public void setUserList(ArrayList<User> list){
        mSelectUserList=list;
    }

    public interface onClickListen {
        void onConfirmListen(ArrayList<User> SelectUserList);
    }
}
